package com.ruoyi.project.warehouse.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.warehouse.domain.WareWarehouseTotal;
import com.ruoyi.project.warehouse.mapper.WareWarehouseTotalMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.warehouse.mapper.WareWarehouseTotalinMapper;
import com.ruoyi.project.warehouse.domain.WareWarehouseTotalin;
import com.ruoyi.project.warehouse.service.IWareWarehouseTotalinService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 总库入库记录Service业务层处理
 *
 * @author You
 * @date 2022-09-04
 */
@Service
public class WareWarehouseTotalinServiceImpl implements IWareWarehouseTotalinService
{
    @Autowired
    private WareWarehouseTotalinMapper wareWarehouseTotalinMapper;

    @Autowired
    private WareWarehouseTotalMapper wareWarehouseTotalMapper;

    /**
     * 查询总库入库记录
     *
     * @param id 总库入库记录主键
     * @return 总库入库记录
     */
    @Override
    public WareWarehouseTotalin selectWareWarehouseTotalinById(Long id)
    {
        return wareWarehouseTotalinMapper.selectWareWarehouseTotalinById(id);
    }

    /**
     * 查询总库入库记录列表
     *
     * @param wareWarehouseTotalin 总库入库记录
     * @return 总库入库记录
     */
    @Override
    public List<WareWarehouseTotalin> selectWareWarehouseTotalinList(WareWarehouseTotalin wareWarehouseTotalin)
    {
        return wareWarehouseTotalinMapper.selectWareWarehouseTotalinList(wareWarehouseTotalin);
    }

    /**
     * 新增总库入库记录
     *
     * @param wareWarehouseTotalin 总库入库记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWareWarehouseTotalin(WareWarehouseTotalin wareWarehouseTotalin)
    {
        /**1.入库操作需要判断入库物品在总库中是否已存在(根据规格编码来判断，需要全等于)
         * 2.存在的话需要在总库的记录总数中加上相应入库数量
         * 3.不存在的话需要在总库中新增一条记录
         * 4.不论此物品在总库中是否存在，在入库记录中都需要新增此条记录
         */
        //1.查询是否存在
        WareWarehouseTotal wareWarehouseTotal = wareWarehouseTotalMapper.findBySpecifications(wareWarehouseTotalin.getSpecifications());
        //创建参数对象
        WareWarehouseTotal param = new WareWarehouseTotal();
        if(wareWarehouseTotal != null){//存在此数据，执行修改
            //计算需要更改的总库数量
            //获取原库存数
            Double oldNum = wareWarehouseTotal.getTotalNum();
            //获取新入库数量
            Double inNum = wareWarehouseTotalin.getInNumber();
            //需要更改的数量
            Double updateNum = oldNum + inNum;
            param.setTotalNum(updateNum);

            //执行修改语句，根据id修改
            param.setId(wareWarehouseTotal.getId());
            wareWarehouseTotalMapper.updateWareWarehouseTotal(param);
        } else {//不存在,执行新增
            param.setName(wareWarehouseTotalin.getName());
            param.setSpecifications(wareWarehouseTotalin.getSpecifications());
            param.setKindCode(wareWarehouseTotalin.getKindCode());
            param.setCategoryCode(wareWarehouseTotalin.getCategoryCode());
            param.setItemCode(wareWarehouseTotalin.getItemCode());
            param.setWbs(wareWarehouseTotalin.getWbs());
            param.setBrand(wareWarehouseTotalin.getBrand());
            param.setUnit(wareWarehouseTotalin.getUnit());
            param.setTotalNum(wareWarehouseTotalin.getInNumber());
            param.setUnitPrice(wareWarehouseTotalin.getUnitPrice());
            param.setTotalPrice(wareWarehouseTotalin.getTotalPrice());
            param.setSupply(wareWarehouseTotalin.getSupply());
            param.setLocation(wareWarehouseTotalin.getLocation());
            param.setOperator(wareWarehouseTotalin.getOperator());
            param.setRemark(wareWarehouseTotalin.getRemark());
            param.setStatus(wareWarehouseTotalin.getStatus());
            param.setCreateTime(DateUtils.getNowDate());
            //执行新增语句
            wareWarehouseTotalMapper.insertWareWarehouseTotal(param);
        }
        //入库记录新增
        wareWarehouseTotalin.setCreateTime(DateUtils.getNowDate());
        return wareWarehouseTotalinMapper.insertWareWarehouseTotalin(wareWarehouseTotalin);
    }

    /**
     * 修改总库入库记录
     *
     * @param wareWarehouseTotalin 总库入库记录
     * @return 结果
     */
    @Override
    public int updateWareWarehouseTotalin(WareWarehouseTotalin wareWarehouseTotalin)
    {
        wareWarehouseTotalin.setUpdateTime(DateUtils.getNowDate());
        return wareWarehouseTotalinMapper.updateWareWarehouseTotalin(wareWarehouseTotalin);
    }

    /**
     * 批量删除总库入库记录
     *
     * @param ids 需要删除的总库入库记录主键
     * @return 结果
     */
    @Override
    public int deleteWareWarehouseTotalinByIds(Long[] ids)
    {
        return wareWarehouseTotalinMapper.deleteWareWarehouseTotalinByIds(ids);
    }

    /**
     * 删除总库入库记录信息
     *
     * @param id 总库入库记录主键
     * @return 结果
     */
    @Override
    public int deleteWareWarehouseTotalinById(Long id)
    {
        return wareWarehouseTotalinMapper.deleteWareWarehouseTotalinById(id);
    }
}
